package utils;

import net.sf.sevenzipjbinding.*;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;

import java.io.*;

/**
 * @author xuguoqing
 * @version 1.0
 * @description: rar压缩工具类
 * @date 2022/10/11 10:30
 */
public class RarUtil {

    //解压rar所用到的方法
    public static void unRarFile(String srcRarPath, String dstDirectoryPath) throws Exception {
        File file = new File(dstDirectoryPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        try(RandomAccessFile randomAccessFile = new RandomAccessFile(new File(srcRarPath), "r");
            IInArchive inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));) {
            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
            //创建文件大小为0的数据
            for (int idx = 0; idx < inArchive.getNumberOfItems(); idx++) {
                String  property = (String)inArchive.getProperty(idx, PropID.PATH);//文件名称
                boolean b1 = property.toLowerCase().endsWith(".rar");
                if (!b1) { //先创建非压缩包内容
                    File tarFile = new File(file + File.separator + property);
                    if (!tarFile.isDirectory()){
                        if (!tarFile.getParentFile().exists()) { //判断父级
                            tarFile.getParentFile().mkdirs();
                        }
                        if (!tarFile.exists()) {
                            if (!(boolean) ((property.contains(".")))) { //判断最后一级为文件夹
                                tarFile.mkdir();
                            }else {tarFile.createNewFile();}
                        }}
                    else {
                        tarFile.mkdir();
                    }
                }
            }
            for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
                File tarFile = new File(file + File.separator + item.getPath());
                //防止包含空文件夹
                item.extractSlow(new ISequentialOutStream() { //开始解压
                    public int write(byte[] data) throws SevenZipException {
                        FileOutputStream fos = null;
                        try {
                            if (!tarFile.getParentFile().exists()) { //判断父级
                                tarFile.getParentFile().mkdirs();
                            }
                            tarFile.createNewFile();
                            fos = new FileOutputStream(tarFile.getAbsolutePath(),true);
                            fos.write(data);
                            fos.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        return data.length;
                    }
                });
            }
        } catch (Exception e) {
           throw e;
        }
    }
}
